package com.irssproject.app.web.action.lectorRSS;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.irssproject.app.entitats.Subscription;
import com.irssproject.app.entitats.User;
import com.irssproject.app.service.lectorRSS.iface.SubscriptionsService;
import com.irssproject.app.utils.Constants;
import com.irssproject.app.web.action.JSONWrappers.FeedJSON;
import com.irssproject.app.web.action.JSONWrappers.SubscriptionJSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RefreshReaderAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = -530591913703967473L;
    SubscriptionsService subscripcioService;

    public RefreshReaderAction(SubscriptionsService subscripcioService) {
        this.subscripcioService = subscripcioService;
    }
    private List<SubscriptionJSON> liSubscripcions = new ArrayList<SubscriptionJSON>();
    private Subscription subscripcio;
    private String feedURL;
    private List<String> llistaIdsSubscripcions;
    private String content;

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setLiSubscripcions(List<SubscriptionJSON> liSubscripcions) {
        this.liSubscripcions = liSubscripcions;
    }

    public List<SubscriptionJSON> getLiSubscripcions() {
        return liSubscripcions;
    }

    public void setFeedURL(String feedURL) {
        this.feedURL = feedURL;
    }

    public String getFeedURL() {
        return feedURL;
    }

    public void setSubscripcio(Subscription subscripcio) {
        this.subscripcio = subscripcio;
    }

    public Subscription getSubscripcio() {
        return subscripcio;
    }

    public String refrescarLector() {
        try {
            Map<String, Object> session = ActionContext.getContext().getSession();

            if (session.containsKey(Constants.SESSIO_USUARI_AUTENTICAT)) {
                User usuari = ((User) session.get(Constants.SESSIO_USUARI_AUTENTICAT));

                String x = (String) llistaIdsSubscripcions.get(0);

                String[] listIdsSubscripcions = x.split(",");

                for (int i = 0; i < listIdsSubscripcions.length; i++) {
                    subscripcioService.refreshSubscription(new Long(listIdsSubscripcions[i]));
                }

                List<Subscription> llistaSubscripcions = subscripcioService.getSubscriptionsList(usuari, null);
                SubscriptionJSON sJSON = new SubscriptionJSON();

                Iterator<Subscription> it = llistaSubscripcions.iterator();

                while (it.hasNext()) {
                    Subscription refrescada = it.next();
                    sJSON = new SubscriptionJSON();
                    sJSON.setId(refrescada.getId());
                    sJSON.setEtiqueta(refrescada.getLabel());
                    sJSON.setFeed(new FeedJSON(refrescada.getFeed()));
                    sJSON.setNumItemsNoLlegits(refrescada.getUnreadItemsNumber());

                    liSubscripcions.add(sJSON);

                }


                setLiSubscripcions(liSubscripcions);

            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        return SUCCESS;
    }

    public void setLlistaIdsSubscripcions(List<String> LlistaIdsSubscripcions) {
        this.llistaIdsSubscripcions = LlistaIdsSubscripcions;
    }

    public List<String> getLlistaIdsSubscripcions() {
        return llistaIdsSubscripcions;
    }
}
